package com.xbongbong.saas.service.impl;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.elasticsearch.model.PaasEsModel;
import com.xbongbong.paas.elasticsearch.pojo.dto.InsertDTO;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.saas.domain.dao.PaymentTaskUserDao;
import com.xbongbong.saas.domain.entity.PaymentTaskUserEntity;
import com.xbongbong.saas.model.PaymentTaskUserModel;
import com.xbongbong.saas.service.PaymentTaskUserService;
import org.elasticsearch.action.support.WriteRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author: 汪腾伟
 * @date: 2020/11/23
 * @desrcption: 回款计划用户实现类
 * @version:1.0
 */
@Service
public class PaymentTaskUserServiceImpl implements PaymentTaskUserService {

    @Resource
    private PaymentTaskUserDao paymentTaskUserDao;

    @Resource
    private PaymentTaskUserModel paymentTaskUserModel;

    @Resource
    private PaasEsModel paasEsModel;
    /**
     * 批量删除回款计划
     *
     * @param dataIdList 回款计划的id列表
     * @param corpid 公司id
     * @throws XbbException
     */
    @Override
    public void deleteBatch(List<Long> dataIdList, String corpid) throws XbbException {
        if(Objects.nonNull(dataIdList)){
            Map<String,Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            map.put("corpid",corpid);
            map.put("del",0);
            map.put("dataIdIn",dataIdList);
            List<PaymentTaskUserEntity> paymentTaskUserEntities = paymentTaskUserDao.findEntitys(map);
            if (CollectionsUtil.isNotEmpty(paymentTaskUserEntities)) {
                List<Long> userIdList = paymentTaskUserEntities.stream().map(u -> u.getId()).collect(Collectors.toList());
                List<InsertDTO> insertList = new ArrayList<>();
                for (PaymentTaskUserEntity paymentTaskUserEntity : paymentTaskUserEntities) {
                    InsertDTO insertDTO = new InsertDTO();
                    insertDTO.setEsId(IndexTypeEnum.IDX_SAAS_PAYMENT_TASK_TEAM.getType() + "_" + corpid + "_" + paymentTaskUserEntity.getId());
                    insertDTO.setParent(corpid + "_" + paymentTaskUserEntity.getDataId());
                    map.put(FieldTypeEnum.UPDATETIME.getAlias(), DateTimeUtil.getInt());
                    map.remove("dataIdIn");
                    insertDTO.setSource(map);
                    insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_PAYMENT_TASK_TEAM);
                    insertList.add(insertDTO);
                }
                paymentTaskUserModel.deleteBatch(userIdList,corpid);
                paasEsModel.updateBatch(insertList, WriteRequest.RefreshPolicy.WAIT_UNTIL);
            }
        }
    }
}
